package com.uenpay.im.ui.old.activity.contacts;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.qyx.android.entity.FriendEntity;
import com.uenpay.im.callback.OnCommonCallBack;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.HttpRequestHandle;
import com.uenpay.im.invokeitems.contacts.AcceptFriendInvokeItem;
import com.uenpay.im.invokeitems.contacts.ApplyAddFriendInvokItem;
import com.uenpay.im.invokeitems.contacts.DeleteFriendsInvokItem;
import com.uenpay.im.invokeitems.contacts.GetUserDetailInvokeItem;
import com.uenpay.im.invokeitems.contacts.ReportFriendsInvokItem;
import com.uenpay.im.invokeitems.contacts.SearchFriendsByPhoneNumInvokeItem;

import java.util.ArrayList;

/**
 * 好友处理
 * 
 * @author SL
 * @created 2016-1-3 上午11:30:58
 * @copyRight http://www.qiyunxin.com
 */
public class ContactsHandle {

	/**
	 * 删除好友
	 * 
	 * @param to_cust_id
	 */
	public static void deleteFriends(String to_cust_id,
			final OnCommonCallBack mBack) {
		DeleteFriendsInvokItem item = new DeleteFriendsInvokItem(to_cust_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							DeleteFriendsInvokItem.DeleteFriendsInvokItemResult result = ((DeleteFriendsInvokItem) resultObject)
									.getOutput();
							mBack.onSuccess(result);
						} else {
							mBack.onFailed(failReason);
						}
					}
				});
	}

	/**
	 * 申请加好友
	 * 
	 * @param cust_id
	 */
	public static void applyAddFriend(final String cust_id,String notes,
			final OnCommonCallBack mOnCommonCallBack) {
		ApplyAddFriendInvokItem item = new ApplyAddFriendInvokItem(cust_id,notes);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							ApplyAddFriendInvokItem.ApplyAddFriendInvokItemResult result = ((ApplyAddFriendInvokItem) resultObject)
									.getOutput();
							mOnCommonCallBack.onSuccess(result);
						} else {
							mOnCommonCallBack.onFailed(failReason);
						}
					}
				});

	}

	/**
	 * 举报
	 * 
	 * @param content
	 *            内容
	 * @param to_cust_id
	 *            用户ID
	 */
	public static void reportFriends(String content, String to_cust_id,
			int spam_type_id, int type, final OnCommonCallBack mBack) {
		ReportFriendsInvokItem item = new ReportFriendsInvokItem(content,
				to_cust_id, spam_type_id, type);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							ReportFriendsInvokItem.ReportFriendsInvokItemResult result = ((ReportFriendsInvokItem) resultObject)
									.getOutput();
							mBack.onSuccess(result);
						} else {
							mBack.onFailed(failReason);
						}
					}
				});
	}

	public interface IAcceptFriendListener {
		void onAcceptFriendResult(int status, String msg,
										 FriendEntity friendEntity);
	}

	/**
	 * 接受好友请求
	 * 
	 * @param notice_id
	 * @param mAcceptFriendListener
	 */
	public static void acceptFriend(String notice_id,
			final IAcceptFriendListener mAcceptFriendListener) {
		AcceptFriendInvokeItem acceptItem = new AcceptFriendInvokeItem(
				notice_id);

		HttpRequestHandle.onBaseRequest(acceptItem,
				HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AcceptFriendInvokeItem.AcceptFriendResult result = ((AcceptFriendInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								mAcceptFriendListener.onAcceptFriendResult(
										result.status, result.msg,
										result.friend);

							} else {
								mAcceptFriendListener.onAcceptFriendResult(-1,
										failReason, null);
							}

						} else {
							mAcceptFriendListener.onAcceptFriendResult(-1,
									failReason, null);
						}
					}
				});
	}

	public interface ISearchAccountListener {
		void onSearchAccountResult(int status, String msg,
										  ArrayList<QYXUserEntity> arrayList);
	}

	/**
	 * 搜素好友
	 * 
	 * @param phoneNum
	 * @param mSearchAccountListener
	 */
	public static void searchAccount(String phoneNum,
			final ISearchAccountListener mSearchAccountListener) {
		SearchFriendsByPhoneNumInvokeItem item = new SearchFriendsByPhoneNumInvokeItem(
				phoneNum);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SearchFriendsByPhoneNumInvokeItem.SearchFriendsByPhoneNumInvokeItemResult result = ((SearchFriendsByPhoneNumInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mSearchAccountListener
										.onSearchAccountResult(result.status,
												result.msg, result.users);
							} else {
								mSearchAccountListener.onSearchAccountResult(
										-1, "", null);
							}
						} else {
							mSearchAccountListener.onSearchAccountResult(-1,
									failReason, null);
						}
					}
				});

	}

	public interface IGetUserDetailListener {
		void onGetUserDetailResult(int status, String msg,
												   QYXUserEntity user, boolean isCache);
	}

	/**
	 * 获取用户资料
	 * 
	 * @param cust_id
	 * @param mGetUserDetailListener
	 */
	public static void getUserDetail(String cust_id,
			final IGetUserDetailListener mGetUserDetailListener) {
		GetUserDetailInvokeItem item = new GetUserDetailInvokeItem(cust_id);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetUserDetailInvokeItem.GetUserDetailInvokeItemResult result = ((GetUserDetailInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mGetUserDetailListener.onGetUserDetailResult(
										result.status, result.msg, result.user,
										isCache);
							} else {
								mGetUserDetailListener.onGetUserDetailResult(
										-1, failReason, null, isCache);
							}
						} else {
							mGetUserDetailListener.onGetUserDetailResult(-1,
									failReason, null, isCache);
						}
					}
				});

	}

}
